package org.example.controller;


import org.example.entity.Dormitory;
import org.example.form.SearchForm;
import org.example.service.DormitoryService;
import org.example.vo.PageVO;
import org.example.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author admin
 * @since 2024-10-31
 */
@RestController
@RequestMapping("/dormitory")
public class DormitoryController {
    @Autowired
    private DormitoryService dormitoryService;
    @GetMapping("/availableList")
    public ResultVO availableList() {
        return dormitoryService.availableList();
    }
    @PostMapping("/save")
    public ResultVO save(@RequestBody Dormitory dormitory) {
        dormitory.setAvailable(dormitory.getType());
        boolean save = this.dormitoryService.save(dormitory);
      if(!save){
          return ResultVO.error("保存失败");
      }else{
          return ResultVO.success(null);
      }
    }
    @GetMapping("/list/{page}/{size}")
    public ResultVO list(@PathVariable int page, @PathVariable int size) {
        PageVO pageVO = this.dormitoryService.list(page, size);
        return ResultVO.success(pageVO);
    }
    @GetMapping("/search")
   public ResultVO search( SearchForm searchForm) {
        PageVO pageVO = this.dormitoryService.search(searchForm);
        return ResultVO.success(pageVO);
    }
    @GetMapping("/findById/{id}")
    public ResultVO findById(@PathVariable int id) {
        Dormitory dormitory = this.dormitoryService.getById(id);
        return ResultVO.success(dormitory);
    }
    @PutMapping("/update")
    public ResultVO update(@RequestBody Dormitory dormitory) {
        boolean update = this.dormitoryService.updateById(dormitory);
        if(!update){
            return ResultVO.error("更新失败");
        }else{
            return ResultVO.success(null);
        }
    }
    @DeleteMapping("/deleteById/{id}")
    public ResultVO delete(@PathVariable int id) {
        boolean remove = this.dormitoryService.deleteById(id);
        if(!remove){
            return ResultVO.error("删除失败");
        }else{
            return ResultVO.success(null);
        }
    }
}

